package cn.edu.swu.gyt.utils;

import cn.edu.swu.gyt.controller.Book;

import java.util.List;

public class HTMLHelper {
    public static String wrapHtml(String content){
        String template=
        """
                <html>
                   <head>
                      <meta charset=utf-8" />
                      <title>西南网上书城</title>
                      <link rel="stylesheet" href="../css/bookstore.css">
                   </head>
                   <body>
                   <center>
                   <br>
                   <h1>欢迎访问西大网上书城</h1>
                   <br>
                   <div>
                   <a href="./add_book.html">添加图书</a>&nbsp;&nbsp;&nbsp;&nbsp;
                   <a href="./books">显示图书</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   <a href="./logout"> 退出系统 </a>
                   <form action="./searchBook" method="get">
                       <input type="text" name="content">&nbsp;&nbsp;&nbsp; <input type="submit" value="查询">
                   </form>
                   </div>
                   <br>
                   %s
                   </center>
                   </body>
                </html>        
                           
        """;
        return String.format(template,content);
    }


    /**
     * 该函数将传入的图书列表转换成一个html的表格
     * @param books
     * @return
     */
    public static String buildBooksTable(List<Book> books){
        StringBuilder sb=new StringBuilder();
        sb.append("<table class='tb-book'>");
        sb.append("<tr>")
                .append("<th>编号</th><th>名字</th><th>作者</th><th>价格</th><th>备注</th><th>日期</th><th>图片</th><th></th><th></th>")
                .append("</tr>");
        String template= """
                   <tr>
                   <td align='center'>%s</td>
                   <td align='center'>%s</td>
                   <td align='center'>%s</td>
                   <td align='center'>%s</td>
                   <td align='center'>%s</td>
                   <td align='center'>%s</td>
                   <td align='center'>%s</td>
                   <td align='center'><a href='./deleteBook?id=%s'>删除</a></td>
                   <td align='center'><a href='./updateBook?id=%s'>修改</a></td>
                   </tr>
                   """;
        for (Book book :books){
            sb.append(
                    String.format(template, book.getId(), book.getName(), book.getAuthor(), book.getPrice(), book.getMemo(), book.getPublish(),(book.getPicture() == null? "" :
                            String.format("<img src='../upload/%s' width='80px'/>", book.getPicture())), book.getId(), book.getId()
                    ));
        }
        sb.append("</table>");
        return sb.toString();
    }
}
